
import re
import urllib.request
import string
import random

 ##获取网页源代码

def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    return html

for i in range(15):#多少页你写多少就行了
    i=i+1
# ------ getHtml()内输入任意帖子的URL ------
    url='https://tieba.baidu.com/p/4910253711?pn='+str(i)
    html = getHtml(url)
# ------ 修改html对象内的字符编码为UTF-8 ------
    html = html.decode('UTF-8')

def getImg(html):
    # ------ 利用正则表达式匹配网页内容找到图片地址 ------
        reg =r'class="BDE_Image" src="(.+?\.jpg)"'
        imgre = re.compile(reg);
        imglist = re.findall(imgre, html)
        return imglist


imgList = getImg(html)
for imgName in imgList:
    print (imgName)
imgName = 0
for imgPath in imgList:
    # ------ 这里最好使用异常处理及多线程编程方式 ------
    salt = ''.join(random.sample(string.ascii_letters + string.digits, 2))  # 防止因为图片重名被覆盖，定义一个随机字符串，加到图片名上
    print(imgPath)
    f = open("pic/" + salt + str(imgName) + ".jpg", 'wb')
    f.write((urllib.request.urlopen(imgPath)).read())
    f.close()
    imgName = imgName + 1

print("已经下载全部图片!")
